/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.Claim;
import entity.ClaimType;
import entity.Hospital;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import sessionBeans.ClaimFacadeLocal;
import java.util.Date;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.faces.model.SelectItem;
import org.primefaces.model.UploadedFile;
import util.SendMail;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class CalimManagement implements Serializable {

    @EJB
    private ClaimFacadeLocal claimFacade;
    private Claim claim = new Claim();
    private Claim selectClaim;
    private List<Claim> list = null;
    private List<Claim> filteredList = null;
    private Hospital hospital = new Hospital();
    private ClaimType claimType = new ClaimType();
    private SelectItem[] statusOption = new SelectItem[]{
        new SelectItem("", "All"),
        new SelectItem("Processing", "Processing"),
        new SelectItem("Agree", "Agree"),
        new SelectItem("Cancel", "Cancel")
    };

    public void create() {
        if (!validate(claim)) {
            return;
        }
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;


        claim.setHospital(hospital);
        claim.setClaimType(claimType);
        claim.setUsers(System.user);
        claim.setDateSend(new Date());

        BigDecimal moneyTemp = claim.getClaimMoney();//Tien Hien Tai Sumbit
        BigDecimal moneyTotal = claimFacade.getClaimMoneyByUserId(System.user.getUserId());//Tien trong nam
        moneyTemp = moneyTemp.add(moneyTotal);
        if (moneyTemp.compareTo(System.user.getJob().getJobLimitPerYear()) == 1) {
            try {
                claim.setClaimStatus("Cancel");
                SendMail.send(System.user.getEmail(), "Alert", "System processed : Cancel your Claim because your amount is greater than your Error Insurance. BillNumber :" + claim.getBillNumber());
                claim.setDateProcesse(new Date());
            } catch (Exception ex) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error Claim", "Error" + ex.getMessage());
            }
        } else {
            claim.setClaimStatus("Processing");
        }
        try {

            claimFacade.create(claim);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Claim", "System send alert about claim to Manager");

        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Create Banking Err", e.getMessage() + "Duplidate Name");
        }
        claim = new Claim();
        list = getCalimList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit() {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            selectClaim.setDateProcesse(new Date());
            SendMail.send(selectClaim.getUsers().getEmail(), "Alert", "System processed : " + selectClaim.getClaimStatus() + " your Claim. BillNumber :" + selectClaim.getBillNumber());
            claimFacade.edit(selectClaim);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit Banking", "Edit Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }
        list = getCalimList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    private List<Claim> getCalimList() {
        if (System.user != null) {
            if (System.Manager) {
                return claimFacade.findAll();
            } else {
                return claimFacade.findByUserIdSend(System.user.getUserId());
            }
        } else {
            return null;
        }

    }

    public Claim getClaim() {

        return claim;
    }

    public void setClaim(Claim claim) {
        this.claim = claim;
    }

    public Claim getSelectClaim() {
        return selectClaim;
    }

    public void setSelectClaim(Claim selectClaim) {
        this.selectClaim = selectClaim;
    }

    public List<Claim> getList() {
        list = getCalimList();
        return list;
    }

    public void setList(List<Claim> list) {
        this.list = list;
    }

    public List<Claim> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<Claim> filteredList) {
        this.filteredList = filteredList;
    }

    public Hospital getHospital() {
        return hospital;
    }

    public void setHospital(Hospital hospital) {
        this.hospital = hospital;
    }

    public ClaimType getClaimType() {
        return claimType;
    }

    public void setClaimType(ClaimType claimType) {
        this.claimType = claimType;
    }

    public SelectItem[] getStatusOption() {
        return statusOption;
    }

    public void setStatusOption(SelectItem[] statusOption) {
        this.statusOption = statusOption;
    }

    //Validate Claim
    private boolean validate(Claim claim) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (claim.getBillNumber() == null || claim.getBillNumber().length() == 0) { //BankName
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BillNumber is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (claim.getBillNumber().length() < 2 || claim.getBillNumber().length() > 10) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BillNumber is from 2 - 10 Numbers");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isInt(claim.getBillNumber())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BillNumber is only Number");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (claim.getClaimMoney() == null || claim.getClaimMoney().toString().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimMoney is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (claim.getClaimMoney().toString().length() > 14) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimMoney is less than 14 Numbers");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isInt(claim.getClaimMoney().toString())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimMoney is only Number");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (claim.getClaimContent() == null || claim.getClaimContent().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimContent is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (claim.getClaimContent().length() < 2 || claim.getClaimContent().length() > 300) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimContent is from 2 - 300 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }

        }
        return true;
    }
}
